<link type="text/css" href="/admins/js/jquery/css/ui-lightness/jquery-ui-1.8.20.custom.css" rel="stylesheet" />
<script type="text/javascript" src="/admins/js/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript" src="/js/boxy/js/jquery.boxy.js"></script>
<link href="/js/boxy/css/boxy.css" media="screen" rel="stylesheet" type="text/css" >  
<script type="text/javascript">
    $(function(){
        window.alert = Boxy.alert;
    });
    <?php if($this->ma == 1) { ?>
        alert("活动添加成功！");
        location.href="/admini/generalize/index";
    <?php }?>
</script>
<div class="admin_nrtop">
    <div class="ad_search" style="font-size: 24px;">
        添加活动
    </div>
</div>
<style>
    .add_div table{width: 960px;font-size: 16px;}
    .add_div table tr{height: 36px;}
    .add_div table .bf{text-align: right;margin-right: 5px;width:120px;}
    .add_div table input{font-size: 16px;width: 280px;}
    .add_div table select{font-size: 16px;width: 280px;}
</style>
<div  class="biaoge">
    <div class="add_div">
        <form action="/admini/generalize/addgeneralizd/" method="post">
            <table border="1" cellpadding="0" cellspacing="0">  
                <tr>
                  <td class='bf'>活动名称：</td>
                  <td class='cf' valign="middle">&nbsp;&nbsp;<input type="text" name="name" id="name"  value=""/></td>
                  <td class='bf'>活动标识：</td>
                  <td class='cf' valign="middle">&nbsp;&nbsp;<input type="text" name="no" id="no"  value=""/></td>
                </tr>
                <tr>
                  <td class='bf'>活动内容：</td>
                  <td class='cf' valign="middle" colspan="3">&nbsp;&nbsp;<textarea id='content' name='content' style='width:760px;height: 100px;margin-top: 10px;font-size: 16px;'></textarea></td>
                </tr>
                <tr>
                  <td class='bf'>开始时间：</td>
                  <td class='cf' valign="middle">&nbsp;&nbsp;<input type="text" name="startTime" id="startTime" value="" readonly onfocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})"/></td>
                  <td class='bf'>结束时间：</td>
                  <td class='cf' valign="middle">&nbsp;&nbsp;<input type="text" name="endTime" id="endTime" value="" readonly onfocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})"/></td>
                </tr>
                <tr>
                    <td class='bf'>活动国家：</td>
                    <td class='cf selCity' valign="middle">&nbsp;&nbsp;<input type="text" name="cityName" id="cityName"  value="全部"/>
                      <input type="hidden" name="cityId" id="cityId"  value="0"/>
                      <div class="guojiaceng" style="display:none;top: 312px;left: 158px;font-size: 14px;">
                        <div class="remen">
                                <h2>热门国家和地区</h2>
                            <ul class="countrylist">
                                <li><a href="javascript:void(0)" id="c_us" cid="0">全部</a></li>
                                <li><a href="javascript:void(0)" cid="1">美国</a></li>
                                <li><a href="javascript:void(0)" cid="2">加拿大</a></li>
                                <li><a href="javascript:void(0)" cid="3">澳大利亚</a></li>
                                <li><a href="javascript:void(0)" cid="47">日本</a></li>
                                <li><a href="javascript:void(0)" cid="39">台湾</a></li>
                                <li><a href="javascript:void(0)" cid="4">新西兰</a></li>
                                <li><a href="javascript:void(0)" cid="5">英国</a></li>
                                <li><a href="javascript:void(0)" cid="42">马来西亚</a></li>
                            </ul>
                        </div>
                        <div class="allcountry">
                            <h2>全部国家和地区</h2>
                            <ul class="countryzm">
                                <?php foreach ($this->words as $char){ ?>
                                    <li <?= $char=='A'?'class="zmon"':'' ?>><a href="javascript:void(0)"><?= $char ?></a></li>
                                <?php } ?>
                            </ul>
                            <div id="countryTab">
                                <?php foreach ($this->words as $char){ ?>
                                <ul class="countrylist"  <?= $char=='A'?'':'style="display:none"' ?>>
                                   <?php $citys= Application_Model_city::GetBySortCode($char);
                                        foreach ($citys as $city){
                                   ?>
                                    <li><a href="javascript:void(0)" cid="<?=$city->Id ?>"><?= $city->Name ?></a></li>
                                    <?php } ?>
                                </ul>
                                <?php } ?>
                            </div>
                        </div>
                    </div>
                  </td>
                  <td class='bf'>状态：</td>
                  <td class='cf' valign="middle">&nbsp;&nbsp;<select id='status' name='status'>
                          <?php foreach ($this->statusAll as $statusKey => $statusVal) { ?>
                              <option value='<?php echo $statusKey;?>'><?php echo $statusVal;?></option>
                          <?php }?>
                      </select></td>
                </tr>
                <tr class="ruleTr1">
                  <td class='cf' colspan="4" >&nbsp;&nbsp;活动规则：
                      <input type="hidden" id="ruleMaxNum" name="ruleMaxNum" value="1" />
                      <a href="javascript:void(0)" class="indexa addRule" style="margin-left:20px;">添加新规则</a>
                  </td>
                </tr>
                <tr class="ruleTr1">
                    <td class='bf'>推广类型：</td>
                    <td class='cf' valign="middle">&nbsp;&nbsp;<select id='grantType' name='grantType[]'>
                            <?php foreach ($this->grantAll as $grantKey => $grantVal) { ?>
                                <option value='<?php echo $grantKey;?>'><?php echo $grantVal;?></option>
                            <?php }?>
                        </select></td>
                    <td class='bf'>礼物类型：</td>
                    <td class='cf' valign="middle">&nbsp;&nbsp;<select id='giftType' name='giftType[]'  i="1">
                            <?php foreach ($this->giftTypeAll as $giftTypeKey => $giftTypeVal) { ?>
                                <option value='<?php echo $giftTypeKey;?>'><?php echo $giftTypeVal;?></option>
                            <?php }?>
                        </select></td>
                </tr>
                <tr class="ruleTr1">
                    <td class='bf'>礼物名称：</td>
                    <td class='cf' valign="middle">&nbsp;&nbsp;<select id='giftId' name='giftId[]'  i="1">
                            <?php foreach ($this->giftAll as $giftVal) { ?>
                                <option value='<?php echo $giftVal->Id;?>'><?php echo $giftVal->Name;?></option>
                            <?php }?>
                        </select></td>
                    <td class='bf'>礼物数量：</td>
                    <td class='cf' valign="middle">&nbsp;&nbsp;<input type="text" id="giftNum" name="giftNum[]" value="0" /></td>
                </tr>
                <tr class="ruleTr1">
                    <td class='bf'>规则限制：</td>
                    <td class='cf' valign="middle">&nbsp;&nbsp;<select id='ruleLimit' name='ruleLimit[]' >
                            <?php foreach ($this->ruleLimitAll as $ruleLimitKey => $ruleLimitVal) { ?>
                                <option value='<?php echo $ruleLimitKey;?>'><?php echo $ruleLimitVal;?></option>
                            <?php }?>
                        </select></td>
                    <td class='bf'>领取张数：</td>
                    <td class='cf' valign="middle">&nbsp;&nbsp;<input type="text" id="ruleNum" name="ruleNum[]" value="" /></td>
                </tr>
                <tr class="ruleTr1">
                    <td class='bf'>最小规则：</td>
                    <td class='cf' valign="middle">&nbsp;&nbsp;<input type="text" id="minRule" name="minRule[]" value="" /></td>
                    <td class='bf'>最大规则：</td>
                    <td class='cf' valign="middle">&nbsp;&nbsp;<input type="text" id="maxRule" name="maxRule[]" value="" /></td>
                </tr>
                <tr class="ruleTr1">
                    <td class='bf'>发放类型：</td>
                    <td class='cf' valign="middle">&nbsp;&nbsp;<select id='generalizeType' name='generalizeType[]'>
                            <?php foreach ($this->generalizeTypeAll as $generalizeKey => $generalizeVal) { ?>
                                <option value='<?php echo $generalizeKey;?>'><?php echo $generalizeVal;?></option>
                            <?php }?>
                        </select></td>
                    <td class="bf">发放时间：</td>
                    <td class="cf" valign="middle">&nbsp;&nbsp;<select id="receiveType" name="receiveType[]">
                            <?php foreach ($this->receiveTypeAll as $receiveKey => $receiveVal) { ?>
                                <option value="<?php echo $receiveKey;?>"><?php echo $receiveVal;?></option>
                            <?php }?>
                        </select></td>
                </tr>
                <tr class="submitTr">
                    <td class='cf' colspan="4"  style="text-align:center;height: 50px;">
                        <input type="submit" id="submit" name="submit" value="添加" style="background-color: #FF8312;width: 100px;height: 36px;color: white;font-size: 16px;border-radius:8px;" />&nbsp;&nbsp;
                        <input type="button" id="rehref" name="rehref" value="返回" style="background-color: #1668A4;width: 100px;height: 36px;color: white;font-size: 16px;border-radius:8px;" /></td>
                </tr>
              </table>
          </form>
    </div>
 </div>
<script>
    $(function(){
        $(".selCity #cityName").click(function(){
            $('.guojiaceng').show();
        });
        $(document).bind("click",function(e){
            var target  = $(e.target);
            if(target.closest(".selCity").length == 0){
                $(".guojiaceng").hide();
            }
        });
        $('.countryzm a').each(function(index,item){
            $(item).click(function(){
                $('.countryzm li.zmon').removeClass('zmon');
                $(this).parent().addClass('zmon');
                $('#countryTab ul').hide();
                $('#countryTab ul').eq(index).show();
                return false;
            });
        });
        $('.countrylist a').bind('click',function(){
             var name = $(this).text();
             var cityId = $(this).attr("cid");
             $("#cityName").val(name);
             $("#cityId").val(cityId);
             $(".guojiaceng").hide();
        });
        $("#giftType").live("change",function(){
            var i = $(this).attr("i");
            var type = $(this).val();
            $.post("/admini/generalize/selgift",{"type":type},function(data){
                if(data.type == 1){
                    var html = "";
                    $.each(data.giftItem,function(giftId,giftName){
                        html += "<option value='"+giftId+"'>"+giftName+"</option>";
                    });
                    $("#giftId[i='"+i+"']").html(html);
                } else {
                    alert("礼物获取不出，请刷新再试！");
                }
            });
        });
        $("#submit").click(function(){
            if($("#name").val() == "") {
                alert("活动名称不能为空！");
                return false;
            } 
            if($("#no").val() == "") {
                alert("活动标识不能为空！");
                return false;
            } 
            if($("#content").val() == "") {
                alert("活动内容不能为空！");
                return false;
            } 
            if($("#startTime").val() == "") {
                alert("活动开始时间不能为空！");
                return false;
            } 
            if($("#endTime").val() == "") {
                alert("活动结束时间不能为空！");
                return false;
            } 
            return true;
        });
        $(".addRule").click(function(){
            var i = $(".ruleTr1 #ruleMaxNum").val();
            i++;
            var html = '<tr class="ruleTr'+i+'"><td class="cf" colspan="4" >&nbsp;&nbsp;新的活动规则：';
            html += '<a href="javascript:void(0)" class="indexa delRule" style="margin-left:20px;" i="'+i+'">删除</a></td></tr>';
            html += '<tr class="ruleTr'+i+'"><td class="bf">推广类型：</td><td class="cf" valign="middle">&nbsp;&nbsp;';
            html += '<select id="grantType" name="grantType[]">';
            <?php foreach ($this->grantAll as $grantKey => $grantVal) { ?>
            html += '<option value="<?php echo $grantKey;?>"><?php echo $grantVal;?></option>';
            <?php }?>
            html += '</select></td><td class="bf">礼物类型：</td><td class="cf" valign="middle">&nbsp;&nbsp;<select id="giftType" name="giftType[]"  i="'+i+'">';
            <?php foreach ($this->giftTypeAll as $giftTypeKey => $giftTypeVal) { ?>
            html += '<option value="<?php echo $giftTypeKey;?>"><?php echo $giftTypeVal;?></option>';
            <?php }?>
            html += '</select></td></tr><tr class="ruleTr'+i+'"><td class="bf">礼物名称：</td>';
            html += '<td class="cf" valign="middle">&nbsp;&nbsp;<select id="giftId" name="giftId[]"  i="'+i+'">';
            <?php foreach ($this->giftAll as $giftVal) { ?>
            html += '<option value="<?php echo $giftVal->Id;?>"><?php echo $giftVal->Name;?></option>';
            <?php }?>
            html += '</select></td><td class="bf">礼物数量：</td><td class="cf" valign="middle">&nbsp;&nbsp;<input type="text" id="giftNum" name="giftNum[]" value="0" /></td></tr>';
            html += '<tr class="ruleTr'+i+'"><td class="bf">规则限制：</td><td class="cf" valign="middle">&nbsp;&nbsp;<select id="ruleLimit" name="ruleLimit[]">';
            <?php foreach ($this->ruleLimitAll as $ruleLimitKey => $ruleLimitVal) { ?>
            html += '<option value="<?php echo $ruleLimitKey;?>"><?php echo $ruleLimitVal;?></option>';
            <?php }?>
            html += '</select></td><td class="bf">领取张数：</td><td class="cf" valign="middle">&nbsp;&nbsp;<input type="text" id="ruleNum" name="ruleNum[]" value="" /></td></tr>';
            html += '<tr class="ruleTr'+i+'"><td class="bf">最小规则：</td><td class="cf" valign="middle">&nbsp;&nbsp;<input type="text" id="minRule" name="minRule[]" value="" /></td>';
            html += '<td class="bf">最大规则：</td><td class="cf" valign="middle">&nbsp;&nbsp;<input type="text" id="maxRule" name="maxRule[]" value="" /></td></tr>';
            html += '<tr class="ruleTr'+i+'"><td class="bf">发放类型：</td><td class="cf" valign="middle">&nbsp;&nbsp;<select id="generalizeType" name="generalizeType[]">';
            <?php foreach ($this->generalizeTypeAll as $generalizeKey => $generalizeVal) { ?>
            html += '<option value="<?php echo $generalizeKey;?>"><?php echo $generalizeVal;?></option>';
            <?php }?>
            html += '</select></td><td class="bf">发放时间：</td><td class="cf" valign="middle">&nbsp;&nbsp;<select id="receiveType" name="receiveType[]">';
            <?php foreach ($this->receiveTypeAll as $receiveKey => $receiveVal) { ?>
            html += '<option value="<?php echo $receiveKey;?>"><?php echo $receiveVal;?></option>';
            <?php }?>
            html += '</select></td></tr>';
            $(".submitTr").before(html);
            $(".ruleTr1 #ruleMaxNum").val(i);
        });
        $(".delRule").live("click",function(){
            var i = $(this).attr("i");
            $(".ruleTr"+i).remove();
        });
        $("#rehref").click(function(){
            history.back();
        }); 
    });
</script>